// Copyright (C) ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
//
// $Id: libxml2_node_ptr.hpp 210 2008-08-28 10:10:41Z heavyzheng $
//

#ifndef ZZHENG_XML_DOM_LIBXML2_NODE_PTR_HPP_20080729__
#define ZZHENG_XML_DOM_LIBXML2_NODE_PTR_HPP_20080729__

#include <zzheng/xml/config.hpp>
#include <libxml/tree.h>

namespace zzheng {
namespace xml {
namespace dom {
namespace libxml2_ {

    //! This class wraps a raw pointer to a libxml2 node. It implements the RAII idiom to help make
    //! our code exception-safe: on destruction, if the raw pointer has not been released, it will
    //! be freed.
    class libxml2_node_ptr {

    public:

        explicit libxml2_node_ptr(xmlNode* px): raw_(px) {
            // Do nothing.
        }

        ~libxml2_node_ptr() {
            if (raw_ != 0) {
                xmlUnlinkNode(raw_);
                xmlFreeNode(raw_);
                raw_ = 0;
            }
        }

        xmlNode* get() const {
            return raw_;
        }

        xmlNode* release() {
            xmlNode* tmp = raw_;
            raw_ = 0;
            return tmp;
        }

    private:

        //! Non-implemented copy constructor.
        libxml2_node_ptr(const libxml2_node_ptr&);

        //! Non-implemented copy assignment.
        libxml2_node_ptr& operator=(const libxml2_node_ptr&);

    private:

        xmlNode* raw_;

    }; // class libxml2_node_ptr

} // namespace zzheng::xml::dom::libxml2_
} // namespace zzheng::xml::dom
} // namespace zzheng::xml
} // namespace zzheng

#endif // ZZHENG_XML_DOM_LIBXML2_NODE_PTR_HPP_20080729__



